package com.gexiaobao.pigeon.ui.fragment.mine.organization.activity

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.ActivityAboutThisSeasonBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterAboutThisSeason
import com.gexiaobao.pigeon.viewmodel.OrgRaceViewModel
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2024/7/17 14:39
 * @Describe    : 关于赛季
 */
class ActivityAboutThisSeason : BaseActivity<OrgRaceViewModel, ActivityAboutThisSeasonBinding>() {

    private var mPassId: Int = 0
    private var mSeason = ""
    private val mAdapter: AdapterAboutThisSeason by lazy { AdapterAboutThisSeason(arrayListOf()) }

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)

        mPassId = intent.getIntExtra("passId", 0)
        mDatabind.swipeRecyclerView.init(LinearLayoutManager(this), mAdapter)
            .addItemDecoration(SpaceItemDecoration(0, Util.dp2px(this, 15f)))

        onRefresh()

        mDatabind.swipeRefreshLayout.init {
            onRefresh()
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            startActivity<ActivityShedIncomeAndExpenditure>(
                "memberId" to mAdapter.data[position].memberId,
                "raceName" to mAdapter.data[position].raceName,
                "memberName" to mAdapter.data[position].memberName,
                "season" to mSeason,
            )
        }
    }

    private fun onRefresh() {
        showLoading(getString(R.string.loading))
        mViewModel.getUserSeasonData(mPassId)
    }

    @SuppressLint("SetTextI18n", "NotifyDataSetChanged")
    override fun createObserver() {
        super.createObserver()
        /** 获取鸽主本赛季数据  */
        mViewModel.aboutSeasonResult.observe(this) {
            if (mDatabind.swipeRefreshLayout.isRefreshing) {
                mDatabind.swipeRefreshLayout.isRefreshing = false
            }
            mSeason = it.season
            mDatabind.includeBar.tvTitle.text = getString(R.string.season_statistics) + "($mSeason)"

            mDatabind.data = it
            mAdapter.setList(it.joinSeasonOrg)
            mAdapter.notifyDataSetChanged()
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack) {
            when (it) {
                mDatabind.includeBar.ivBack -> finish()
            }
        }
    }
}